package com.lyh.web.controller.pay;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyh.common.annotation.Log;
import com.lyh.common.core.controller.BaseController;
import com.lyh.common.core.domain.AjaxResult;
import com.lyh.common.core.domain.entity.SysUser;
import com.lyh.common.core.page.TableDataInfo;
import com.lyh.common.enums.BusinessType;
import com.lyh.common.enums.PaymentMethodEnum;
import com.lyh.common.enums.RechargeStatusEnum;
import com.lyh.common.enums.RechargeTypeEnum;
import com.lyh.common.utils.SecurityUtils;
import com.lyh.common.utils.poi.ExcelUtil;
import com.lyh.system.domain.RechargeRecord;
import com.lyh.system.domain.UserCashAdvance;
import com.lyh.system.domain.req.*;
import com.lyh.system.domain.resp.UserCashAdvancePageResp;
import com.lyh.system.domain.vo.AllCashAdvanceRecordVo;
import com.lyh.system.domain.vo.CashAdvanceVo;
import com.lyh.system.mapper.UserCashAdvanceMapper;
import com.lyh.system.service.ICashAdvanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/pay/cashAdvance")
@Api(value = "提现管理", description = "提现管理")
public class CashAdvanceController extends BaseController {


    @Autowired
    private ICashAdvanceService cashAdvanceService;
    @Autowired
    private UserCashAdvanceMapper userCashAdvanceMapper;

    @GetMapping("/pageList")
    @ApiOperation("分页管理")
    public TableDataInfo pageList(UserCashAdvancePageReq req) {
        startPage();
        List<UserCashAdvancePageResp> list = cashAdvanceService.selectPageList(req);
        return getDataTable(list);
    }

    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody UserCashAdvancePageReq req) {
        req.setPageNum(null);
        req.setPageSize(null);
        List<UserCashAdvancePageResp> list = cashAdvanceService.selectPageList(req);

        ExcelUtil<UserCashAdvancePageResp> util = new ExcelUtil<>(UserCashAdvancePageResp.class);
        util.exportExcel(response, list, "提现明细导出");
    }


    @ApiOperation("提现审核")
    @PostMapping("/audit")
    @Log(title = "提现审核", businessType = BusinessType.UPDATE)
    public AjaxResult audit(@Validated @RequestBody MemberPurchasAuditReq req) {
        return  cashAdvanceService.audit(req);
    }

    @GetMapping("/webNum")
    @ApiOperation("提现汇总")
    public AjaxResult webNum(UserCashAdvancePageReq req) {
        UserCashAdvancePageResp userCashAdvancePageResp = userCashAdvanceMapper.selectAllNum(req);
        return AjaxResult.success(userCashAdvancePageResp.getAdvanceMoney());
    }

    @ApiOperation("批量提现审核")
    @PostMapping("/batchAudit")
    @Log(title = "批量提现审核", businessType = BusinessType.UPDATE)
    public AjaxResult audit(@Validated @RequestBody MemberPurchasBatchAuditReq req) {
        cashAdvanceService.batchAudit(req);
        return AjaxResult.success();
    }

    @Log(title = "提现编辑", businessType = BusinessType.UPDATE)
    @ApiOperation("提现编辑")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MemberPurchasEditReq req) {
        return toAjax(cashAdvanceService.edit(req));
    }

    @Log(title = "提现申请", businessType = BusinessType.INSERT)
    @ApiOperation("提现申请")
    @PostMapping("/apply")
    public AjaxResult apply(@Validated @RequestBody AdvanceApplyReq req) {
        return toAjax(cashAdvanceService.apply(req));
    }

    @GetMapping("/list")
    @ApiOperation("App提现列表")
    public AjaxResult list() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<UserCashAdvance> userCashAdvances = userCashAdvanceMapper.selectList(new LambdaQueryWrapper<UserCashAdvance>()
                .eq(UserCashAdvance::getUserId, user.getUserId()));

        BigDecimal allChanges = new BigDecimal(0);
        BigDecimal allAccountBalance = new BigDecimal(0);

        List<CashAdvanceVo> list = new ArrayList<>();
        for (UserCashAdvance userCashAdvance : userCashAdvances) {

            if ("changes".equals(userCashAdvance.getAdvanceSource())) {
                allChanges = allChanges.add(userCashAdvance.getAdvanceMoney());
            } else if ("account_balance".equals(userCashAdvance.getAdvanceSource())) {
                allAccountBalance = allAccountBalance.add(userCashAdvance.getAdvanceMoney());
            }

            CashAdvanceVo ca = new CashAdvanceVo();
            ca.setPrice(userCashAdvance.getAdvanceMoney());
            ca.setType(userCashAdvance.getAdvanceType());
            ca.setSource(userCashAdvance.getAdvanceSource());
            ca.setWithdrawalTime(userCashAdvance.getCreateTime());
            ca.setStatus(userCashAdvance.getStatus());
            list.add(ca);
        }

        list = list.stream().sorted(Comparator.comparing(CashAdvanceVo::getWithdrawalTime).reversed()).collect(Collectors.toList());
        AllCashAdvanceRecordVo advanceRecord = new AllCashAdvanceRecordVo();
        advanceRecord.setCashAdvanceVoList(list);
        advanceRecord.setAllChanges(allChanges);
        advanceRecord.setAllAccountBalance(allAccountBalance);
        return AjaxResult.success(advanceRecord);
    }
}
